package com.songlanyun.modules.memberbill.controller.api;

import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.memberbill.entity.*;
import com.songlanyun.modules.memberbill.service.SaleRefillCardBillService;
import com.songlanyun.modules.memberbill.service.SaleRefillVoucherBillService;
import com.songlanyun.modules.openSwitch.mapper.YxtSwitchMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 充值卡账单
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-16 14:58:05
 */
@RestController
@RequestMapping("api/bill/refillVoucher")
public class SaleRefillVoucherBillApiController extends AbstractApiController {
    @Autowired
    private SaleRefillVoucherBillService saleRefillVoucherBillService;

    @Value("${balance.exchange.passport.enabled}")
    private boolean balanceExchangePassportEnabled;

    @Autowired
    private YxtSwitchMapper yxtSwitchMapper;

    /**
     * 查询自己的账单 分页
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params) {
        PageUtils page = saleRefillVoucherBillService.getUserBillPage(getUserId(true), params);

        return R.ok().put("page", page);
    }

    /**
     * 查询自己的账单
     */
    @RequestMapping("/list")
    public R list() {
        List<SaleRefillVoucherBillEntity> list = saleRefillVoucherBillService.getUserBill(getUserId(true));

        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SaleRefillVoucherBillEntity saleRefillCardBill = saleRefillVoucherBillService.getById(id);

        return R.ok().put("data", saleRefillCardBill);
    }


    /**
     * 积分转余额
     * @param dto
     * @return
     */
    @PostMapping("/pointToBalance")
    public R pointToBalance(@RequestBody PointToBalanceDTO dto) {
//        ValidatorUtils.validateEntity(dto);
//        saleRefillCardBillService.pointToBalance(getUserId(true),dto);
//        return R.ok();
        return R.error("该功能已关闭");
    }

    /**
     * 积分转余额计算
     * @param dto
     * @return
     */
    @PostMapping("/pointToBalanceComputer")
    public R pointGiveAwayComputer(@RequestBody PointToBalanceDTO dto) {
//        throw new RRException("该功能已关闭");
        PointToBalanceComputerVO vo = saleRefillVoucherBillService.pointGiveAwayComputer(getUserId(true),dto);
        return R.ok().put("data", vo);
    }

    /**
     * 余额转赠
     * @param dto
     * @return
     */
    @PostMapping("/balanceGiveAway")
    public R balanceGiveAway(@RequestBody BalanceGiveAwayDTO dto) {
        ValidatorUtils.validateEntity(dto);
        saleRefillVoucherBillService.balanceGiveAway(getUserId(true),dto);
        return R.ok();
    }

    /**
     * 余额转积分
     * @param dto
     * @return
     */
    @PostMapping("/balanceToPoint")
    public R balanceToPoint(@RequestBody BalanceToPointDTO dto) {
        Integer balanceExchangeAssport = yxtSwitchMapper.selectYxtSwitchById(1L).getBalanceExchangeAssport();
        if(balanceExchangeAssport == 0){
            throw new RRException("该功能暂时关闭");
        }
        ValidatorUtils.validateEntity(dto);
        saleRefillVoucherBillService.balanceToPoint(getUserId(true), dto);
        return R.ok();
    }

    /**
     * 余额转积分计算
     * @param dto
     * @return
     */
    @PostMapping("/balanceToPointComputer")
    public R balanceToPointComputer(@RequestBody BalanceToPointDTO dto) {
        BalanceToPointComputerVO vo = saleRefillVoucherBillService.balanceToPointComputer(getUserId(true),dto);
        return R.ok().put("data", vo);
    }

}
